package com.yunhe.uitl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DruidTest {
    static ClassPathXmlApplicationContext cpac;
    static {
        cpac=new ClassPathXmlApplicationContext("spring.xml");
    }
    @Test
    public void druid() throws SQLException {
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
    }
    @Test
    public void druid1() throws SQLException {
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");

        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(rb.getString("jdbc.driver"));
        dataSource.setUrl(rb.getString("jdbc.url"));
        dataSource.setUsername(rb.getString("jdbc.username"));
        dataSource.setPassword(rb.getString("jdbc.password"));

        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
    }





    @Test
    public void spring_druid() throws SQLException {
        DataSource ds = (DataSource) cpac.getBean("druid");
        Connection connection = ds.getConnection();
        System.out.println(connection);
    }
}
